<template>
  <uni-forms ref="uFormRef" :modelValue="formData" :rules="rules">
    <uni-forms-item name="deviceSn">
      <template #label>
        <view class="nw-label">烹饪设备</view>
      </template>
      <view class="uss-form-select">
        <nw-PickerSelect v-model="formData.deviceSn" :columns="[deviceList]" style="width: 100%; max-width: 100%"
          @confirm="handleDeviceConfirm" />
      </view>
    </uni-forms-item>
    <uni-forms-item name="cookingMethod">
      <template #label>
        <view class="nw-label">执行动作</view>
      </template>
      <view class="uss-form-select">
        <nw-PickerSelect v-model="formData.method" :columns="[g_options.methodOptions]"
          style="width: 100%; max-width: 100%" />
      </view>
    </uni-forms-item>
    <uni-forms-item name="recipeId">
      <template #label>
        <view class="nw-label">操作内容</view>
      </template>
      <nw-Input v-model="formData.name" label="操作内容" />
    </uni-forms-item>
    <uni-forms-item>
      <template #label>
        <view class="nw-label">数量</view>
      </template>
      <view class="nw-value">
        {{
          (formData.value ? formData.value : '') ||
            '--'
        }}
        {{ formData.value ? formData.unit : '' }}
      </view>
    </uni-forms-item>
  </uni-forms>
</template>

<script setup>
import useNumberConvert from '../../mixins/useNumberConvert'

const props = defineProps({
  voiceData: {
    type: Object,
    default: () => ({})
  },
  analysisSource: {
    type: Object,
    default: () => ({})
  }
})

const uFormRef = ref(null)
const formData = ref({
  deviceSn: [],
  method: ['0'],
  name: '',
  code: '',
  driverId: '',
  value: '',
  unit: '',
  action: ''
})

// 校验规则
const rules = {
  deviceSn: {
    rules: g_rules.selectRule('烹饪设备')
  },
  method: {
    rules: g_rules.selectRule('执行动作')
  },
  name: {
    rules: g_rules.selectRule('操作内容')
  },
  value: {
    rules: g_rules.selectRule('数量')
  }
}

const deviceStore = useDeviceStore()


const { bindDeviceList } = storeToRefs(deviceStore)
const deviceList = computed(() => {
  return g_.cloneDeep(bindDeviceList.value).filter(item => {
    if ([g_i.device.DeviceStatus.Idle, g_i.device.DeviceStatus.ManualCooking].includes(item.status * 1)) {
      item.value = item.sn
      item.label = item.name
      return item
    }
  })
})

const { convertNumber } = useNumberConvert()

// 处理手动语音指令
const dealVoiceParams = params => {
    let {object, param, ...exras} = params
    console.log('================ 语音处理前', object, param)
    const pattern = /[\u4e00-\u9fa5A-Za-z]/g
    const wantParam = param.replace(pattern, '') * 1
    console.log('================ 语音处理后', object, wantParam)
    formData.value.deviceSn = [props.analysisSource.deviceSn]
    if (object.includes('火力')) {
        formData.value.value = wantParam > 6 ? 6 : wantParam
        formData.value.name = '火力'
        formData.value.method = ['3']
        formData.value.action = 'handleFirepowerChange'
        
    } else if (object.includes('拌刀')) {
        if (param.includes('正')) {
            formData.value.value = wantParam > 6 ? 6 : wantParam
            formData.value.name = '拌刀-正'
            formData.value.action = 'handleForwardSpeedChange'
            formData.value.method = ['3']
            // formData.value.reverseSpeed = 0
        } else if (param.includes('反')) {
            formData.value.value = wantParam > 6 ? 6 : wantParam
            formData.value.name = '拌刀-反'
            formData.value.action = 'handleReverseSpeedChange'
            formData.value.method = ['3']
            // formData.value.forwardSpeed = 0
        } else if (param.includes('停止')) {
            formData.value.value = 0
            formData.value.name = '拌刀-停止'
            formData.value.action = 'handleForwardSpeedChange'
            formData.value.method = ['3']
            // formData.value.forwardSpeed = 0
            // formData.value.reverseSpeed = 0
            // handleForwardSpeedChange(0)
        } else if (param.includes('放下')) {
          formData.value.value = '放下'
          formData.value.name = '拌刀'
          formData.value.action = 'handleSpatulaSwitch'
          formData.value.code = 1
          formData.value.method = ['1']
          
        } else if (param.includes('抬起')) {
          formData.value.value = '抬起'
          formData.value.name = '拌刀'
          formData.value.action = 'handleSpatulaSwitch'
          formData.value.code = 2
          formData.value.method = ['2']
          
        }
    } else if (object.includes('食材')) {
      const tempObject = convertNumber(object)
      const food = props.analysisSource.foodBoxList.find(val => val.name === tempObject) || {}
      formData.value.value = 1
      formData.value.name = food.name
      formData.value.driverId = food.driverId
      formData.value.code = food.type
      formData.value.action = 'handleClickLaunch'
      formData.value.method = ['0']
    } else {
      const boxes = props.analysisSource.spiceBoxList.filter(
        val => {
            return val.ingredientName === object
        }
      )|| []
      if (boxes?.length > 0) {
        formData.value.value = wantParam
        formData.value.name = boxes[0].ingredientName
        formData.value.driverId = boxes[0].driverId
        formData.value.code = boxes[0].type
        formData.value.action = 'handleClickLaunch'
        formData.value.method = ['0']
      }
    }
    
}

watch(() => props.voiceData, (n,o) => {
  console.log(n)
  dealVoiceParams(n)
}, {
  immediate: true
})

const handleDeviceConfirm = e => {
  console.log('handleDeviceConfirm', e, formData.value)
  const value = e.value[0]
  formData.value.deviceSn = [value.sn]
}



const done = () => {
  return uFormRef.value.validate().then((res => {
    return Promise.resolve(formData.value)
  }))
}

defineExpose({done})


</script>